﻿using System;
using System.Collections.Generic;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace testyaml
{
    public class Address
    {
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }

        public void Save(string path)
        {
            var serializer = new SerializerBuilder()
                .WithNamingConvention(CamelCaseNamingConvention.Instance)
                .Build();
            var yaml = serializer.Serialize(this);
            System.Console.WriteLine(yaml);
            File.WriteAllText(path, yaml);
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public float HeightInInches { get; set; }
        public Dictionary<string, Address> Addresses { get; set; }

        public void Save(string path)
        {
            var serializer = new SerializerBuilder()
                .WithNamingConvention(CamelCaseNamingConvention.Instance)
                .Build();
            var yaml = serializer.Serialize(this);
            System.Console.WriteLine(yaml);
            File.WriteAllText(path, yaml);
        }

    }

    public class Program
    {
        public static void Main()
        {
            var person = new Person
            {
                Name = "Abe Lincoln",
                Age = 25,
                HeightInInches = 6f + 4f / 12f,
                Addresses = new Dictionary<string, Address>
                {
                    {
                        "home",
                        new Address()
                        {
                            Street = "2720  Sundown Lane",
                            City = "Kentucketsville",
                            State = "Calousiyorkida",
                            Zip = "99978",
                        }
                    },
                    {
                        "work",
                        new Address()
                        {
                            Street = "1600 Pennsylvania Avenue NW",
                            City = "Washington",
                            State = "District of Columbia",
                            Zip = "20500",
                        }
                    },
                },
            };

            var person2 = new Person
            {
                Name = "SS fsdg",
                Age = 252,
                HeightInInches = 12f + 4f / 12f,
                Addresses = new Dictionary<string, Address>
                {
                    {
                        "home",
                        new Address()
                        {
                            Street = "2ytryen Lane",
                            City = "ewtuetsville",
                            State = "Calewtkida",
                            Zip = "996588",
                        }
                    },
                    {
                        "work",
                        new Address()
                        {
                            Street = "1666nsylvania 78W",
                            City = "Wreyon",
                            State = "Disyuudof Columbia",
                            Zip = "20350",
                        }
                    },
                },
            };
            Console.WriteLine("a???????????");

            person.Save("./ss.txt");
            Console.WriteLine("b???????????");
            person.Addresses.First().Value.Save("./ss.txt");

            Console.WriteLine("???????????end");

            var listser = new List<Person>() { person, person2 };

            var serializer = new SerializerBuilder()
                .WithNamingConvention(CamelCaseNamingConvention.Instance)
                .Build();
            var yaml = serializer.Serialize(listser);
            System.Console.WriteLine(yaml);
            // Output:
            // name: Abe Lincoln
            // age: 25
            // heightInInches: 6.3333334922790527
            // addresses:
            //   home:
            //     street: 2720  Sundown Lane
            //     city: Kentucketsville
            //     state: Calousiyorkida
            //     zip: 99978
            //   work:
            //     street: 1600 Pennsylvania Avenue NW
            //     city: Washington
            //     state: District of Columbia
            //     zip: 20500

            var yml =
                @"
-   name: George Washington
    age: 89
    height_in_inches: 5.75
    addresses:
      home:
        street: 400 Mockingbird Lane
        city: Louaryland
        state: Hawidaho
        zip: 99970
-   name: ewt rwyryhington
    age: 29
    height_in_inches: 4.75
    addresses:
      home:
        street: 424de
        city: L455s
        state: Ha0902kaho
        zip: 930
";

            var deserializer = new DeserializerBuilder()
                .WithNamingConvention(UnderscoredNamingConvention.Instance)
                .Build();

            //yaml contains a string containing your YAML
            var pp = deserializer.Deserialize<List<Person>>(yml);
            foreach (var p in pp)
            {
                var h = p.Addresses["home"];
                System.Console.WriteLine(
                    $"{p.Name} is {p.Age} years old and lives at {h.Street} in {h.City}, {h.State}."
                );
            }
            // Output:
            // George Washington is 89 years old and lives at 400 Mockingbird Lane in Louaryland, Hawidaho.
        }
    }
}
